package com.bootdo.common.task;

import com.bootdo.system.dao.BeautyDeptDao;
import com.bootdo.system.dao.DeptDao;
import com.bootdo.system.dao.WeekDao;
import com.bootdo.system.domain.BeautyDeptDO;
import com.bootdo.system.domain.DeptDO;
import com.bootdo.system.domain.WeekDO;
import org.apache.shiro.util.CollectionUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 每周最后一天生成班级得分记录
 */
@Component
public class BeautyDeptJob implements Job {

    @Autowired
    private DeptDao deptDao;

    @Autowired
    private WeekDao weekDao;

    @Autowired
    private BeautyDeptDao beautyDeptDao;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        Map<String,Object> map = new HashMap<String,Object>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String weekEndDate = formatter.format(new Date());
        map.put("weekEndDate",weekEndDate);
        //查询每周周期
        List<WeekDO> list = weekDao.list(map);
        if(!CollectionUtils.isEmpty(list)) {
            //查询班级得分情况
            List<DeptDO> list1 = deptDao.list(map);
            BeautyDeptDO beautyDeptDO = new BeautyDeptDO();
            beautyDeptDO.setWeekId(list.get(0).getWeekId());
            beautyDeptDO.setWeekName(list.get(0).getWeekName());
            beautyDeptDO.setWeekNumber(list.get(0).getWeekNumber());
            beautyDeptDO.setWeekStartDate(list.get(0).getWeekStartDate());
            beautyDeptDO.setWeekEndDate(list.get(0).getWeekEndDate());
            if(!CollectionUtils.isEmpty(list1)) {
                list1.forEach(data -> {
                    beautyDeptDO.setDeptId(data.getDeptId());
                    beautyDeptDO.setParentId(data.getParentId());
                    beautyDeptDO.setName(data.getName());
                    beautyDeptDO.setScore(data.getScore());
                    //每周最后一天生成班级得分记录
                    beautyDeptDao.save(beautyDeptDO);
                });
            }
        }
    }
}
